package com.gjx.clock.ui

import android.content.Intent
import android.os.Bundle
import android.view.Gravity
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.*
import androidx.core.view.isVisible
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.gjx.clock.ClockViewModel
import com.gjx.clock.NormalAdapter
import com.gjx.clock.R
import com.gjx.clock.entity.NormalAlarmClock
import com.gjx.clock.service.ForwardService
import com.google.android.material.floatingactionbutton.FloatingActionButton
import kotlinx.android.synthetic.main.fragment_normal_alarm_clock.*
import java.util.*
import kotlin.collections.ArrayList
import kotlin.concurrent.thread
import android.content.ComponentName
import android.net.Uri
import android.os.Build
import android.provider.Settings
import com.gjx.clock.BuildConfig
import java.lang.Exception


class NormalAlarmClockFragment : Fragment() {

    private lateinit var adapter: NormalAdapter
    private val viewModel by lazy { ViewModelProvider(this).get(ClockViewModel::class.java) }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

    }

    // 每次从别的界面返回，都刷新一下数据
    override fun onResume() {
        super.onResume()
        // 从数据库获取闹钟,用主线程
        viewModel.loadNormalClocks()
        adapter.notifyDataSetChanged()
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {


        val view = inflater.inflate(R.layout.fragment_normal_alarm_clock, container, false)


        val recyclerView = view.findViewById(R.id.normalRecyclerView) as RecyclerView
        val layoutManager = LinearLayoutManager(context)
        recyclerView.layoutManager = layoutManager
        adapter = NormalAdapter(viewModel, this, viewModel.normalClockList)
        recyclerView.adapter = adapter

        val addNormalBtn = view.findViewById<FloatingActionButton>(R.id.addNormalBtn)

        // 添加闹钟
        addNormalBtn.setOnClickListener {
            val intent = Intent(context,NormalClockSettingsActivity::class.java)
            intent.putExtra("addAlarm",true)
            startActivity(intent)

        }

        return view
    }



    override fun onDestroy() {
        super.onDestroy()
    }
}